package com.example.smartcity.ui.home.adapter

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.smartcity.R
import com.example.smartcity.SmartCityApplication.Companion.context
import com.example.smartcity.ui.home.model.MoreEntity
import com.google.android.material.bottomnavigation.BottomNavigationView

/**
 *  ANDROID STUDIO @version 2022.1
 *          @email 2506789532@qq.com  @ikun
 *  time: 2023/1/22 12:09
 **/
class MoreRecyclerViewAdapter(val fragment: Fragment, private val list: List<MoreEntity>) : RecyclerView.Adapter<MoreRecyclerViewAdapter.HolderView>(){

    inner class HolderView(view: View) : RecyclerView.ViewHolder(view){
        // 获取控件
        val imgView = view.findViewById<ImageView>(R.id.re_imgView)
        val tvTitle = view.findViewById<TextView>(R.id.re_tvTitle)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HolderView {
        val root = LayoutInflater.from(parent.context).inflate(R.layout.recyclerview_item_more, parent,false)
        val view = HolderView(root)

        view.itemView.setOnClickListener {
            if(list[view.position].title == "全部服务")  {
                val bnv = fragment.requireActivity().findViewById<BottomNavigationView>(R.id.nav_view)
                bnv.selectedItemId = R.id.navigation_more
            }
        }

        return view
    }

    override fun getItemCount() = list.size

    override fun onBindViewHolder(holder: HolderView, position: Int) {
        val data = list[position]
        if(data.imageView.isEmpty()){
            holder.imgView.setImageResource(R.drawable.more)
        }else {
            Glide.with(context)
                .load(data.imageView)
                .into(holder.imgView)
        }
        holder.tvTitle.text = data.title
    }
}